package com.moody.init;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.moody.hibernate.domain.CodeLib;

/*
 * Copyright © Moody analytics   
 *
 * @Title: Guaranty.java 
 * @Package： com.moody.init 
 * @author： Fuqiang Wang
 * @Description: TODO(保证类型：大类和子类)    
 * @date： 2012-3-28 下午1:54:24
 * @Modified by: Fuqiang Wang 
 * @version V1.0   
 */

public class Guaranty {
	private static final Logger log = LoggerFactory.getLogger(Guaranty.class);
	private static Guaranty instance = null;
	private static Map<String, Map<String, String>> guarantyTreeMap = new LinkedHashMap<String, Map<String, String>>();
	private static Map<String, String> guarantyMap = new LinkedHashMap<String, String>();
	private static LinkedList<LinkedHashMap<String, String>> guarantyLevelOneList = new LinkedList<LinkedHashMap<String, String>>();

	private Guaranty() {

	}

	public static Guaranty getInstance() {
		if (instance == null) {
			instance = new Guaranty();
		}
		return instance;
	}

	public Map<String, String> getGuaranty() {
		Map<String, CodeLib> industry = new LinkedHashMap<String, CodeLib>();
		industry = DataDictionary.getInstance().getDataDictionary()
				.get("GuarantyType");
		Iterator iter = industry.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String industryCode = (String) entry.getKey();
			String industryName = ((CodeLib) entry.getValue()).getItemName();
			guarantyMap.put(industryCode, industryName);
		}
		return guarantyMap;
	}

	public Map<String, Map<String, String>> getGuarantyTree() {
		Map<String, CodeLib> industry = new LinkedHashMap<String, CodeLib>();

		industry = DataDictionary.getInstance().getDataDictionary()
				.get("GuarantyType");

		Iterator iter = industry.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			String guarantyCode = (String) entry.getKey();
			String guarantyName = ((CodeLib) entry.getValue()).getItemName();
			if (guarantyCode.length() == 2
					&& !guarantyTreeMap.containsKey(guarantyCode)) {
				guarantyTreeMap.put(guarantyCode,
						new LinkedHashMap<String, String>());
			} else if (guarantyCode.length() == 4) {
				String guarantyCodeLevelOne = guarantyCode.substring(0, 2);
				// String industryNameLevelOne =
				// industry.get(industryCodeLevelOne);
				if (guarantyTreeMap.containsKey(guarantyCodeLevelOne)
						&& !guarantyTreeMap.get(guarantyCodeLevelOne)
								.containsKey(guarantyCode)) {
					guarantyTreeMap.get(guarantyCodeLevelOne).put(guarantyCode,
							guarantyName);
				} else if (!guarantyTreeMap.containsKey(guarantyCodeLevelOne)) {
					Map<String, String> guarantyLevelTwo = new LinkedHashMap<String, String>();

					guarantyLevelTwo.put(guarantyCode, guarantyName);
					guarantyTreeMap.put(guarantyCodeLevelOne, guarantyLevelTwo);

				}
			}

		}
		return guarantyTreeMap;
	}

	@SuppressWarnings("unchecked")
	public LinkedList<LinkedHashMap<String, String>> getGuarantyLevelOne() {
		if (guarantyLevelOneList != null && guarantyLevelOneList.size() > 0)
			return guarantyLevelOneList;
		else {
			Map<String, CodeLib> guaranty = DataDictionary.getInstance()
					.getDataDictionary().get("GuarantyType");
			Iterator iter = guaranty.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry<String, CodeLib> entry = (Entry<String, CodeLib>) iter
						.next();
				String guarantyCode = (String) entry.getKey();
				String guarantyName = ((CodeLib) entry.getValue())
						.getItemName();
				if (guarantyCode.length() == 2) {
					LinkedHashMap<String, String> itemMap = new LinkedHashMap<String, String>();
					itemMap.put("code", guarantyCode);
					itemMap.put("name", guarantyName);
					guarantyLevelOneList.add(itemMap);
				}
			}
			return guarantyLevelOneList;
		}
	}

	public List<Map<String, String>> getGuarantyLevelTwo(String levelOneId) {
		List<Map<String, String>> levelTwoList = new LinkedList<Map<String, String>>();
		getGuarantyTree();
		Map<String, String> levelTwo = guarantyTreeMap.get(levelOneId);
		Iterator iter = levelTwo.entrySet().iterator();
		while (iter.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
					.next();
			String code = entry.getKey();
			String name = entry.getValue();
			Map<String, String> itemMap = new LinkedHashMap<String, String>();
			itemMap.put("code", code);
			itemMap.put("name", name);
			levelTwoList.add(itemMap);
		}
		return levelTwoList;
	}
}
